/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Buslog;

import ActiveRecord.Newdb.Db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author degrave
 */
public class Calendar {

    protected int id;
    protected int eventId;
    protected String ownerName;
    //public LinkedList<Event> eventList;

    public Calendar(int newid, String name, int eventId) {
        this.id = newid;
        this.ownerName = name;
        this.eventId = eventId;
        Event event = new Event();
        event.ownerCalendarId = eventId;
    }

    public int getId() {
        return id;
    }

    public void setId(int newid) {
        this.id = newid;
        //OrderQueryStub.alterOrder(this);
    }

    public int geteventId() {
        return eventId;
    }

    public void seteventId(int newid) {
        this.eventId = newid;
        //OrderQueryStub.alterOrder(this);
    }

    public String getOwnerName() {
        return ownerName;
    }

    public void setOwnerName(String name) {
        this.ownerName = name;
        //OrderQueryStub.alterOrder(this);
    }

    public void pushToDB() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            s.executeUpdate("insert into CALENDARS (id, ownerName, eventId)"
                    + " values (" + id + ",'" + ownerName + "'," + eventId + ")");
            System.out.println("inserted into calendars table");
        } catch (SQLException ex) {
            System.out.println("Cant insert into calendars table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void removeFromDB() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            s.executeUpdate("delete from CALENDARS where id = " + id);
            System.out.println("Removed from calendars table");
        } catch (SQLException ex) {
            System.out.println("Cant remove from calendars table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static int getMaxId() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select MAX(id) from CALENDARS");
            if (rs.next()) {
                return rs.getInt(1);
            } else {
                return 0;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }

    public static Calendar findCalendar(int id, int key) {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs;
            if (key == 1) {
                rs = s.executeQuery("select * from CALENDARS where id = " + id);
                if (rs.next()) {
                    Calendar cal = new Calendar(rs.getInt(1), rs.getString(2), rs.getInt(3));
                    System.out.println("Find data from calendars table");
                    return cal;
                }
            }
            if (key == 2) {
                rs = s.executeQuery("select * from CALENDARS where eventId = " + id);
                if (rs.next()) {
                    Calendar cal = new Calendar(rs.getInt(1), rs.getString(2), rs.getInt(3));
                    System.out.println("Find data from calendars table");
                    return cal;
                }
            } else {
                System.out.println("Cant find data from calendars table");
                return (new Calendar(0, "", 0));
            }
        } catch (SQLException ex) {
            System.out.println("Cant find data from calendars table");
            Logger.getLogger(ActiveRecord.UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (new Calendar(0, "", 0));
    }
}
